--- title: ImageFilter Data Augmentation keywords: fastai sidebar: home_sidebar summary: "Apply `PIL.ImageFilter`s to your input images" description: "Apply `PIL.ImageFilter`s to your input images" nb_path: "nbs/augment_albumentations.ipynb" ---
%load_ext autoreload
%autoreload 2
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
files = get_image_files('../assets/imgs')
f1 = files[0]
f2 = files[1]
img1 = PIL.Image.open(f1).resize((500,300))
img2 = PIL.Image.open(f2).resize((500,300))
img1
img2
to_img = lambda x: PILImage.create(x)
as_arr = lambda x: np.array(x)
aug = lambda op,img: op(image=as_arr(img))['image']
aug_img = lambda op,img: to_img(aug(op,img))
side_by_side = lambda arr1,arr2: np.hstack((arr1,arr2))
compare = lambda op,img: PIL.Image.fromarray(side_by_side(img, aug(op,img)))
def compose_augs(augs:list, img:PIL.Image.Image):
if not isinstance(augs, list): augs=[augs]
for aug in augs:
img = aug_img(aug,img)
return img
blur = Blur(always_apply=True)
compare(blur, img2)
motion_blur = MotionBlur(blur_limit=(15,15), always_apply=True)
compare(motion_blur, img1)
median_blur = MedianBlur(blur_limit=(3,7), always_apply=True)
compare(median_blur, img1)
emboss = IAAEmboss(strength=(0.2,0.99), always_apply=True)
compare(emboss, img1)
pca = FancyPCA(0.3, always_apply=True)
compare(pca, img2)
solarize = A.Solarize(always_apply=True)
compare(solarize, img2)
gray = ToGray(always_apply=True)
compare(gray, img2)
sepia = ToSepia(always_apply=True)
compare(sepia, img2)
tfm = IAAAdditiveGaussianNoise(always_apply=True)
compare(tfm, img2)
gauss_noise = GaussNoise(always_apply=True)
compare(gauss_noise, img2)
jpeg_compress = JpegCompression(quality_lower=25, quality_upper=55, always_apply=True)
compare(jpeg_compress, img2)
posterize = Posterize(True)
compare(posterize, img1)
rain = A.RandomRain(always_apply=True, blur_value=3)
compare(rain, img2)
snow = A.RandomSnow(always_apply=True)
compare(snow, img2)
fog = A.RandomFog(always_apply=True)
compare(fog, img1)
sun_flare = RandomSunFlare(always_apply=True, src_radius=80)
compare(sun_flare, img2.resize((224,224)))
random_contrast = RandomContrast(always_apply=True)
compare(random_contrast, img1)
random_brightness = RandomBrightness(0.05, always_apply=True)
compare(random_brightness, img1)
clahe = CLAHE(always_apply=True)
compare(clahe, img2)
channel_shuffle = ChannelShuffle(always_apply=True)
compare(channel_shuffle,img2)
hsv = HueSaturationValue(val_shift_limit=10)
compare(hsv, img1)
rgb_shift = RGBShift(always_apply=True)
compare(rgb_shift, img2)
The transforms below are segregated into 8 different blocks:
HueSaturationValue has a low val_shift_limit to
minimise lighting change, as that will come from other
lighting transforms
albu_tfms = AlbumentationsWrapper(Tfms)
hstack([albu_tfms(f2), albu_tfms(f2), albu_tfms(f2)])
hstack([albu_tfms(f2), albu_tfms(f2), albu_tfms(f2)])
hstack([albu_tfms(f2), albu_tfms(f2), albu_tfms(f2)])